using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Xml;

namespace Universe.Farm.Service
{
    public class Configuration
    {
        public IList<Listener> Listeners = new List<Listener>();

        public CommunicationParameters Communication = CommunicationParameters.Default;

        public class Listener
        {
            public string TypeName;
            public int Interval = -1;
            public string Id;
            public XmlElement Options;

            public Listener()
            {
            }

            public Listener(string typeName, int interval, string id, XmlElement options)
            {
                TypeName = typeName;
                Interval = interval;
                Id = id;
                Options = options;
            }


            public Listener(Type type, int interval): this(type)
            {
                Interval = 1;
            }

            public Listener(Type type)
            {
                TypeName = type.AssemblyQualifiedName;

                XmlDocument doc = new XmlDocument();
                doc.AppendChild(doc.CreateElement("Listeneers"));
                XmlElement el1 = doc.CreateElement("Listeneer");
                doc.DocumentElement.AppendChild(el1);
                el1.SetAttribute("type", TypeName);
                Options = doc.DocumentElement;
            }
        }



        public class CommunicationParameters
        {
            public bool IsBroadcast;
            public int Port;
            public ICollection<IPAddress> IgnoreList = new List<IPAddress>();
            public ICollection<IPAddress> PeerList = new List<IPAddress>();
            public IPAddress Group;

            public int ServicePort;
            public ICollection<IPAddress> ServiceAddress = new List<IPAddress>();

            public static CommunicationParameters Default
            {
                get
                {
                    CommunicationParameters ret = new CommunicationParameters();
                    ret.IsBroadcast = true;
                    ret.ServiceAddress.Add(IPAddress.Any);
                    ret.ServicePort = ret.Port = DefaultPort;
                    return ret;
                }
            }

            public static readonly int DefaultPort = 50505;
        }

    }
}
